﻿using System;

namespace FractionNumber
{
    public class Fraction : IFormattable
    {
        private int numerator;
        private int denominator;

        public Fraction(int numerator, int denominator)
        {
            if (denominator == 0)
            {
                throw new ArgumentException("The denominator cannot be 0.");
            }
            this.numerator = numerator;
            this.denominator = denominator;
            if (denominator < 0)
            {
                this.numerator *= -1;
                this.denominator *= -1;
            }
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                    return ToString();
                case "N":
                    return string.Format("{0}/{1}", numerator, denominator);
                case "R":
                    return string.Format("{0:F2}", ((double)numerator) / denominator);
                default:
                    throw new ArgumentException("Invalid format.");
            }
        }

        public override string ToString()
        {
            return (((double)numerator) / denominator).ToString();
        }
    }
}
